package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.ArtTypeMapper;
import com.hbnu.system.model.entity.Art_Type;
import com.hbnu.system.model.vo.ChildrenNameId;
import com.hbnu.system.model.vo.NameId;
import com.hbnu.system.service.IArtTypeService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArtTypeServiceImpl extends ServiceImpl<ArtTypeMapper, Art_Type> implements IArtTypeService {
    @Override
    public List<ChildrenNameId> getCascadingData() {
        HashMap<Integer, String> type_map_4 = new HashMap<>();
        HashMap<Integer, String> type_map_3 = new HashMap<>();
        HashMap<Integer, String> type_map_2 = new HashMap<>();
        HashMap<String, Integer> type_map_1 = new HashMap<>();
//        List<NameId> lastList = new ArrayList<>();
//        List<NameId> thridList = new ArrayList<>();
//        List<NameId> secondList = new ArrayList<>();
//        List<ChildrenNameId> result = new ArrayList<>();
        HashSet<NameId> lastList = new HashSet<>();
        HashSet<NameId> thridList = new HashSet<>();
        HashSet<NameId> secondList = new HashSet<>();
        HashSet<ChildrenNameId> result = new HashSet<>();

        List<Art_Type> art_types = baseMapper.selectList(Wrappers.lambdaQuery(Art_Type.class));
        for (Art_Type art_type : art_types) {
            if(art_type.getType4ID() != 0)
                type_map_4.put(art_type.getType4ID(),art_type.getArtType4());
            if(art_type.getType3ID() != 0)
                type_map_3.put(art_type.getType3ID(),art_type.getArtType3());
            if(art_type.getType2ID() != 0)
                type_map_2.put(art_type.getType2ID(),art_type.getArtType2());
            if(art_type.getType1ID() != 0)
                type_map_1.put(art_type.getArtType1(),art_type.getType1ID());
        }
//        System.out.println("type4=" + type_map_4.size() + type_map_4);
//        System.out.println("type3=" + type_map_3.size() + type_map_3);
//        System.out.println("type2=" + type_map_2.size() + type_map_2);
//        System.out.println("type1=" + type_map_1.size() + type_map_1);
        // 第四类type全部设置为NameId 放入列表
        for (Map.Entry<Integer,String> entry : type_map_4.entrySet()) {
            if(entry.getKey() != 0){
                NameId nameId = new NameId(entry.getValue(), entry.getKey());
                lastList.add(nameId);
            }
        }

        // 第三类type看情况创建 顺便将NameId元素放入children列表
        for (Map.Entry<Integer,String> entry : type_map_3.entrySet()) {
             if((entry.getKey() >= 211 && entry.getKey()<= 212) || (entry.getKey() >= 311 && entry.getKey() <= 347)){ // 这就是要创建ChildrenNameId的
                 ChildrenNameId childrenNameId = new ChildrenNameId(entry.getValue(), entry.getKey());
                 for (NameId nameId : lastList) {
                     if(Integer.parseInt(String.valueOf(nameId.getValue()).substring(0,3)) == entry.getKey()){ // 如果对上号了
                         if(!childrenNameId.getChildren().contains(nameId)){ // 只有在不包括的情况下才执行这个操作
                             childrenNameId.getChildren().add(nameId);
                             thridList.add(childrenNameId);
                         }

                     }
                 }
             }else {
                 NameId nameId = new NameId(entry.getValue(), entry.getKey());
                 thridList.add(nameId);
             }
        }

        //第二类type看情况创建 顺便将NameId元素放入children列表
        for (Map.Entry<Integer,String> entry : type_map_2.entrySet()) {
            if(entry.getKey() == 25 || entry.getKey() == 26 || entry.getKey() == 28  || entry.getKey() == 29  || entry.getKey() == 210
                    || entry.getKey() == 15 || entry.getKey() == 16 || entry.getKey() == 17 || entry.getKey() == 18 || entry.getKey() == 19 || entry.getKey() == 110
                    || entry.getKey() == 111 || entry.getKey() == 112){
                NameId nameId = new NameId(entry.getValue(), entry.getKey());
                secondList.add(nameId);
            }else {
                ChildrenNameId childrenNameId = new ChildrenNameId(entry.getValue(), entry.getKey());
                for (NameId nameId : thridList) {
                    if (childrenNameId.getValue() == nameId.getValue()/10){
                        if (!childrenNameId.getChildren().contains(nameId)){
                            childrenNameId.getChildren().add(nameId);
                        }
                    }
                }
                secondList.add(childrenNameId);
            }

        }

        //第一类type看情况创建 顺便将NameId元素放入children列表
        for (Map.Entry<String,Integer> entry : type_map_1.entrySet()) {
            ChildrenNameId childrenNameId = new ChildrenNameId(entry.getKey(), entry.getValue());
            for (NameId nameId : secondList) {
                if (childrenNameId.getValue() == Integer.parseInt(String.valueOf(nameId.getValue()).substring(0,1))){
                    if (!childrenNameId.getChildren().contains(nameId)){
                        childrenNameId.getChildren().add(nameId);
                    }
                }
            }
            result.add(childrenNameId);
        }

//        HashSet<ChildrenNameId> resultSet = new HashSet<>(result);
        ArrayList<ChildrenNameId> resultNew = new ArrayList<>(result);

        return resultNew;
    }
}
